package com.lzx.iteam.net;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Message;
import android.text.TextUtils;

import com.lzx.iteam.CloudDialerActivity;
import com.lzx.iteam.DialerApp;
import com.lzx.iteam.bean.CloudContact;
import com.lzx.iteam.bean.PhoneNumItem;
import com.lzx.iteam.util.Constants;
import com.lzx.iteam.util.PhoneNumberUtils;
import com.lzx.iteam.util.StringUtil;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by shijunting on 15-5-13.
 */
public class GetContactListMsg extends CcMsgStructure {

    private Message mmCallback;

    public GetContactListMsg(Message callback) {
        mmCallback = callback;
    }

    @Override
    public void onReceive(JSONObject data) {
        ArrayList<CloudContact> contactList = new ArrayList<CloudContact>();
        try {
            String groupid = "";
            String tagid = "";

            JSONArray jContactList = data.getJSONArray(AsynHttpClient.KEY_CC_DATA);
            for (int i = 0; i < jContactList.length(); i++) {
                JSONObject jContact = jContactList.getJSONObject(i);
                CloudContact contact = new CloudContact();
                contact.groupId = jContact.getString(AsynHttpClient.KEY_GROUP_ID);
                contact.userId = jContact.getString(AsynHttpClient.KEY_USER_ID);//add by lk
                contact.contactName = jContact.getString(AsynHttpClient.KEY_CONTACT_NAME);
                contact.contactWork = "";
                if (jContact.has(AsynHttpClient.KEY_CONTACT_WORK)){
                    contact.contactWork = jContact.getString(AsynHttpClient.KEY_CONTACT_WORK);
                }
//                contact.contactRemarks = jContact.getString(AsynHttpClient.KEY_CONTACT_REMARKS);
                contact.contact_image = jContact.getString(AsynHttpClient.KEY_CONTACT_IMAGE);
                //                    contact.contactUrl = jContact.getString(AsynHttpClient.KEY_CONTACT_URL);
//                    contact.contactedCount = jContact.getInt(AsynHttpClient.KEY_CONTACT_DIALCOUNT);
                String jNumListStr = jContact.getString(AsynHttpClient.KEY_CONTACT_PHONENUMBER);
                contact.contactPhoneNum = new ArrayList<PhoneNumItem>();
                if ((!TextUtils.isEmpty(jNumListStr)) && (!jContact.isNull(AsynHttpClient.KEY_CONTACT_PHONENUMBER))) {
                    JSONArray jNumListArr = new JSONArray(jNumListStr);
                    /*for (int j = 0; j < jNumListArr.length(); j += 2) {
                        PhoneNumItem phoneItem = new PhoneNumItem();
                        phoneItem.label = jNumListArr.getString(j);
                        phoneItem.phoneNum = jNumListArr.getString(j + 1);
                        contact.contactPhoneNum.add(phoneItem);
                    }*/
                    PhoneNumItem phoneItem = new PhoneNumItem();
                    phoneItem.label = jNumListArr.getString(0);
                    phoneItem.phoneNum = PhoneNumberUtils.cutPlus86Formatter(jNumListArr.getString(1));
                    contact.contactPhoneNum.add(phoneItem);
                }
//                contact.contact_phone=jContact.getString(AsynHttpClient.KEY_CONTACT_PHONE);
//                contact.contact_landline=jContact.getString(AsynHttpClient.KEY_CONTACT_LANDLINE);
                contact.contactId = jContact.getString(AsynHttpClient.KEY_CONTACT_ID);
                contact.userType = jContact.getString(AsynHttpClient.KEY_USER_TYPE);
                contactList.add(contact);

                groupid = contact.groupId;
                tagid = jContact.getString("tag_id");
            }

            if (tagid.equals("0")) {
                CloudDialerActivity.instance.rootcontactList = contactList;
            } else if (CloudDialerActivity.instance.chooseelement != null) {
                CloudDialerActivity.instance.chooseelement.contactList = contactList;         //instance.chooseelement.c
            }


            SharedPreferences sp = DialerApp.mContext.getSharedPreferences("structure", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sp.edit();
            editor.putString("groupid" + groupid + "tagid" + tagid, jContactList.toString());
            //editor.commit();


            //SharedPreferences.Editor editor = sp.edit();
            editor.putBoolean("groupid" + groupid + "tagid" + tagid + "tag", true);
            editor.commit();


            if (mmCallback != null) {
                mmCallback.arg1 = 1;
                mmCallback.obj = contactList;
                mmCallback.sendToTarget();
                return;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

       /* if (mmCallback != null) {
            mmCallback.arg1 = -1;
            mmCallback.obj = contactList;
            mmCallback.sendToTarget();
        }*/
    }

    @Override
    public void onError(int error, Object ret) {
        String errorMsg = null;
        if (ret != null) {
            JSONObject jData = null;
            try {
                jData = new JSONObject(ret.toString());
                errorMsg = jData.getString("msg");
            } catch (JSONException e) {
                errorMsg = "服务端数据解析失败!";
                e.printStackTrace();
            }

        }
        if (mmCallback != null) {
            mmCallback.arg1 = error;
            if (!StringUtil.isEmpty(errorMsg)) {
                mmCallback.obj = errorMsg;
            } else {
                mmCallback.obj = Constants.NETWORK_WRROR;
            }
            mmCallback.sendToTarget();
        }

    }
}
